<template>
	<eui-background type="common">
		<view class="approve-body">
			<eui-nav-bar :title="(approveInfo.BillId || '') + '详情'"
				color="#fff" rightIcon="" />
			<view class="main-view">
				<BillDetail v-if="detailMobilePageGuid" parentType="approveManger" :bill-id="approveInfo.BillId"
					:-lowcode-guid="approveInfo.MdCode" :-lowcode-detail-guid="detailMobilePageGuid" />
				<component :is="componentName" :data="approveInfo" />
			</view>
			<view class="user-footer" :style="{marginBottom:AreaBottom+'px'}">
				<ApproveBottomBar :data="approveInfo" :isStatus="true" :tabIndex="tabIndex" @change="onRefresh"
					@changeWebview="WebviewControl">
				</ApproveBottomBar>
			</view>
		</view>
	</eui-background>
</template>

<script setup type="ts">
	import { ref,getCurrentInstance } from 'vue';
	import { onLoad,onReady } from '@dcloudio/uni-app';
	import ApproveBottomBar from'@/components/ApproveBottomBar.vue';
	import service from '@/common/service';
	import Enjoy from '@/common/enjoy';
	import { useMainStore } from '@/store/modules/store';
	import { useLoginStore } from '@/store/modules/useLoginStore';
	import { tempsComponents } from '@/hooks/useApproveTempsComponents';
	import BillDetail from "../../MainViews/lowcode/billDetail/index.vue";
  	import { init } from '@/hooks/init';
	import eventBus from '@/pages/MainViews/eventBus';

	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	const AreaBottom = ref(uni.getSystemInfoSync().safeAreaInsets.bottom ? uni.getSystemInfoSync().safeAreaInsets.bottom : 0);
	const approveInfo = ref({});
	const cEmbed = ref({});
	const tabIndex = ref('');
	const detailMobilePageGuid = ref('');
	const componentName = ref('');

	const MobileApprovePage = () =>{
		service.enjoyShowLoading('正在加载...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40审批",
		        ObjectData: {
					AppId: approveInfo.value.AppId,
					ReqType: 0
				},
		        MethodName: "MobileApprovePage",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					UpdateApproveReadStatus();
					const ObjectData = res.data.ObjectData;
					// console.log("ObjectData", JSON.stringify(ObjectData));
					if (ObjectData?.DetailMobilePageGuid && +ObjectData?.RetType === 5) {
						detailMobilePageGuid.value = ObjectData?.DetailMobilePageGuid;
						// const lowcodeGuid = approveInfo.value?.MdCode, billId = approveInfo.value?.BillId;
						// const lowcodeGuid = ((ObjectData?.BillContent || "") + "")?.startsWith("mobile_") ? ObjectData?.BillContent?.slice?.(7) : ObjectData?.BillContent;
						// uni.navigateTo({ url: `../lowcode/billDetail/index?billId=${billId}&LowcodeGuid=${lowcodeGuid}&LowcodeDetailGuid=${ObjectData?.DetailMobilePageGuid}` });
						service.enjoyHideLoading();
						return;
					}
					if(ObjectData.RetType==4){
						componentName.value=tempsComponents[ObjectData.BillContent];
						service.enjoyHideLoading();
						return;
					}
					cEmbed.value = plus.webview.create(
						'/hybrid/html/OAWebview/index.html',
						'index.html', {
							top: uni.getSystemInfoSync().statusBarHeight + 54,
							bottom: uni.getSystemInfoSync().safeAreaInsets.bottom + 60,
							left:10,
							right:10,
						}, {
							selectedItem: {
								AppId:approveInfo.value.AppId,
								AppRecvId:approveInfo.value.AppRecvId,
								ObjectData:JSON.stringify(ObjectData),
								loginInfo:JSON.stringify(mainStore.loginInfo),
								EnjoyOA:LoginStore.EnjoyMain,
								EnjoyRMIS:LoginStore.EnjoyRMIS,
								EnjoyMall:LoginStore.EnjoyMall,
								EnjoyCRM:LoginStore.EnjoyCRM,
								ProjectName:approveInfo.value.ProjectName,
								MdCode:approveInfo.value.MdCode,
							},
						},
					);
					cEmbed.value.onloaded=(scuss)=>{
						console.log("scuss: " + JSON.stringify(scuss));
					};
					cEmbed.value.onerror=(err)=>{
						console.log("err: " + JSON.stringify(err));
					};
					let pages = getCurrentPages();
					let currentPage = pages[pages.length - 1];
					var currentWebview = currentPage.$getAppWebview();
					currentWebview.append(cEmbed.value);
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}

	const UpdateApproveReadStatus = () => {
		// console.log(118, approveInfo.value.ReadStatus, '__________________________________', approveInfo.value);
		if (approveInfo.value.ReadStatus == '未阅') {
			service.enjoyShowLoading('正在处理...');
			let request = {
				url: LoginStore.EnjoyMain,
				data: {
					UniqueKey: "管易通40审批",
					ObjectData: {
						AppRecvId: approveInfo.value.AppRecvId,
						MsgRecvId: approveInfo.value.MsgRecvId,
						MsgId: approveInfo.value.MsgId,
						AppUserNo: mainStore.loginInfo.sUser
					},
					MethodName: "UpdateApproveReadStatus",
				},
			}
			service.request(request, (res) => {
				service.enjoyHideLoading();
				if (res && res.data) {
					if (!res.data.HasException) {
						eventBus.emit('refresh-HomeApprove');
						setTimeout(() => {
							SetCurrentPages();
						}, 300)
					} else {
						service.enjoyShowToast(res.data.Exception.Message)
					}
				}
			}, () => {
				service.enjoyHideLoading();
				console.log('失败')
			});
		}
	}
	const WebviewControl = e =>{
		if(e){
			cEmbed.value.show()
		}else{
			cEmbed.value.hide()
		}
	}
	const onRefresh = () =>{
		console.log(123131321);
		setTimeout(() => {
			SetCurrentPages();
			uni.navigateBack({
				delta: 1
			});
		}, 600)
	}
	const SetCurrentPages = () =>{
		try {
			let pages2 = getCurrentPages();
			let prevPage2 = pages2[pages2.length - 2];
			prevPage2.$vm.$.exposed.onRefresh();
			prevPage2.$vm.$.exposed.SetCurrentPages();
		} catch (e) {
		}
	}
	onLoad(async e=>{
		const customerObject = service.getStorageSync("CUSTOMEROBJECT");
		if (customerObject && customerObject.EnjoyRDS != undefined) {
			await init();
		}
		approveInfo.value = JSON.parse(e.item);
		tabIndex.value = e.tabIndex;
		MobileApprovePage();
	})
	defineExpose({
		onRefresh,
		SetCurrentPages
	})
</script>

<style lang="scss" scoped>
	@import '@/main.scss';

	.content-img {
		background-color: #FFFFFF;
		background-repeat: no-repeat;
		width: 100%;
	}

	.main-view {
		flex: 1;
		flex-direction: column;
		// padding: 20rpx;
		background-color: #f2f2f2;
	}

	.user-footer {
		// position: fixed;
		// z-index: 11;
		// bottom: 0;
		// left: 0;
		// right: 0;
		background-color: #fff;
		padding: 8px 10px;
	}

	.approve-body {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
	}
</style>
